package com.camerareader.mvp.image_list_page.extras;

import android.content.pm.ProviderInfo;
import android.mtp.MtpObjectInfo;
import android.nfc.Tag;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.fileoperation.model.Photo;
import com.jakewharton.rxbinding.view.RxView;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import rx.functions.Action1;

/**
 * Created by Administrator on 2016-12-06.
 */

public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ThumbnailItemViewHolder> {

    private static final String TAG = "ImageListAdapter";
    private ArrayList<Photo> list;
    private ArrayList<Integer> selectedArray = new ArrayList<>();
    private boolean allSelected = false;
    private boolean couldSelected = false;

    public boolean isAllSelected() {
        return allSelected;
    }

    public ImageListAdapter(boolean couldSelected) {
        this.couldSelected = couldSelected;
    }

    public void setList(ArrayList<Photo> list) {
        this.list = list;
        selectedArray.clear();
        allSelected = false;
        notifyDataSetChanged();
    }

    public void setAllSelected(boolean allSelected) {
        this.allSelected = allSelected;
        selectedArray.clear();
        notifyDataSetChanged();
    }

    @Override
    public ThumbnailItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ThumbnailItemViewHolder(new ImageItemView(parent.getContext()));
    }

    public ArrayList<Photo> getSelectedImageList() {
        if (allSelected){
            return list;
        }
        if (selectedArray.size() == 0) {
            return null;
        } else {
            ArrayList<Photo> list = new ArrayList<>(selectedArray.size());
            for (int m : selectedArray) {
                list.add(this.list.get(m));
            }
            return list;
        }
    }

    @Override
    public void onBindViewHolder(ThumbnailItemViewHolder holder, int position) {
        System.out.println("--------onbind------------");
        holder.thumbnail.setImageResource(list.get(position).getPath(), list.get(position).getFileName());

        holder.thumbnail.setStateSelected(allSelected || selectedArray.contains(position));
        Log.w(TAG, list.get(position).toString());

    }

    @Override
    public int getItemCount() {
        if (list == null) return 0;
        return list.size();
    }

    class ThumbnailItemViewHolder extends RecyclerView.ViewHolder {
        ImageItemView thumbnail;
        ThumbnailItemViewHolder(View itemView) {
            super(itemView);
            this.thumbnail = (ImageItemView) itemView;
            if (couldSelected){
                RxView.clicks(itemView).throttleFirst(1, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        int index = ThumbnailItemViewHolder.this.getAdapterPosition();
                        if (selectedArray.contains(index)){
                            selectedArray.remove(Integer.valueOf(index));
                            thumbnail.setStateSelected(false);
                        }else {
                            selectedArray.add(index);
                            thumbnail.setStateSelected(true);
                        }
                    }
                });
            }

        }
    }

    public interface OnStateChangedListener {

    }
}
